package com.swu.pad.comment.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.swu.pad.comment.dao.ConnectDatabase;

/**
 * 存储评论内容，因为插件原因不能使用SpringMVC，只能使用Servlet来存储
 * 
 * @author yuge
 *
 */
@Controller
public class SavaComment extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// @Autowired
	// private FbCommentService fbCommentService;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 2 接收给属性赋值
		String commentId = request.getParameter("commentId");// 评论在服务器上的id
		String type = request.getParameter("type");// 评论属于哪一类
		String feedbackType = request.getParameter("radioValue");
		int feedbackScore = Integer.parseInt(request.getParameter("replayStar").toString());
		String timestamp = request.getParameter("timestamp");// 时间戳
		String authorId = request.getParameter("AuthorID");// 被评论而且要给出评论分数的s人
		String commenterId = request.getParameter("Commenter");
		String padId = request.getParameter("padId");
		padId = padId == null ? "" : new String(padId.getBytes("ISO-8859-1"), "UTF-8");
		String commentText = request.getParameter("text");
		commentText = commentText == null ? "" : new String(commentText.getBytes("ISO-8859-1"), "UTF-8");
		// 获得与数据库的连接项
		Connection conn = null;// 连接数据库
		PreparedStatement pstmt = null;

		// 向数据库插入数据
		try {
			// 插入comments语句
			String sql = "insert into fbcomment (comment_id,type,feedback_type,feedback_score,timestamp,commenter_id,author_id,comment_text,pad_id) values(?,?,?,?,?,?,?,?,?)";
			// 获得与数据库的连接
			conn = ConnectDatabase.getConnection();
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			pstmt.setString(1, commentId);
			pstmt.setString(2, type);
			pstmt.setString(3, feedbackType);
			pstmt.setInt(4, feedbackScore);
			pstmt.setString(5, timestamp);
			pstmt.setString(6, commenterId);// 内容
			pstmt.setString(7, authorId);
			pstmt.setString(8, commentText);
			pstmt.setString(9, padId);
			pstmt.executeUpdate();// 执行语句update
			// 插入成功
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("向数据库插入fbcomments发生异常！");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
